package com.yunji.hushuang;

import java.io.*;

/**
 * @author hushuang
 * @since 2019/10/14
 */
public class CloneUtil {

    private CloneUtil() {
    }

    @SuppressWarnings("unchecked")
    public static <T extends Serializable> T deepClone(T object) {
        try (ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
             ObjectOutputStream out = new ObjectOutputStream(byteOut)) {
            out.writeObject(object);

            try (ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
                 ObjectInputStream in = new ObjectInputStream(byteIn)) {
                return (T) in.readObject();
            }
        } catch (Exception e) {
            throw new RuntimeException("Clone Failed", e);
        }
    }
}
